/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaTipoHabitacion.java
 *
 * Created on 24/11/2010, 01:45:55 AM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.service.HabitacionService;

/**
 *
 * @author elvis
 */
public class ListaTipoHabitacion extends javax.swing.JInternalFrame {

   private JDesktopPane escritorio;
   private List<Tipohabitacion> listaTipos;
   private HabitacionService habitacionService;

    /** Creates new form ListaTipoHabitacion */
    public ListaTipoHabitacion() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTipos = new javax.swing.JTable();
        btnNuevo = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Lista Tipo de Habitacion");

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel1.setText("Tipo de Habitaciones");

        tblTipos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Codigo", "Nombre", "Descripcion"
            }
        ));
        jScrollPane1.setViewportView(tblTipos);

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btneliminar.setText("Eliminar");
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                .addComponent(btnModificar)
                .addGap(46, 46, 46)
                .addComponent(btneliminar)
                .addGap(38, 38, 38))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jLabel1)
                .addContainerGap(148, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevo)
                    .addComponent(btneliminar)
                    .addComponent(btnModificar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
      FormTipoHabitacion form = new FormTipoHabitacion();
      form.setTipohabitacion(new Tipohabitacion());
      form.cargaInicial();
      form.setEscritorio(escritorio);
      escritorio.add(form);
      form.setVisible(true);
      this.dispose();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
       if (tblTipos.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado una categoria");
         return;
       }
      FormTipoHabitacion form = new FormTipoHabitacion();
      form.setTipohabitacion( listaTipos.get(tblTipos.getSelectedRow()) );
      form.cargaInicial();
      form.setEscritorio(escritorio);
      escritorio.add(form);
      form.setVisible(true);
      this.dispose();
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
         if (tblTipos.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ningun tipo de habitacion");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar el tipo de habitacion?","Eliminar Tipo de habitacion ",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Tipohabitacion dt = listaTipos.get(tblTipos.getSelectedRow());
         dt.setEstado(0);
         habitacionService.registrarTipoHabitacion(dt);
         JOptionPane.showMessageDialog(this, "Se ha eliminado el tipo de habitacion seleccionado");
         listaTipos = habitacionService.listaTipoHabitacion();
         pintagrilla();
       }
    }//GEN-LAST:event_btneliminarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btneliminar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTipos;
    // End of variables declaration//GEN-END:variables


   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   public void cargaInicial(){
      if(habitacionService==null){
         habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");
      }
      if(listaTipos == null ){
         listaTipos = habitacionService.listaTipoHabitacion();
      }
      pintagrilla();
   }

   public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblTipos.getModel();
        tabla.setRowCount(0);
        for (Tipohabitacion tipo : listaTipos) {
            Object[] fila = new Object[3];
            fila[0] = tipo.getIdtipoHabitacion();
            fila[1] = tipo.getNombre();
            fila[2] = tipo.getDetalle();
            tabla.addRow(fila);
        }

      }

}
